var base_url_indexAdvPic = '/rs/adPics';
var base_url_citys = '/rs/openCity';
var base_url_city = '/rs/city';

var pics = [];
var citys = [];
var operation = "add";
var currentPageNo = 1;
var pageRows = 10;

$(function() {
    authName = getCookie('username');
    userCity = getCookie('usercity');
    userCategory = getCookie('usercategory');

    if(userCategory == 9) {
        queryCitys();
    }
    else if(userCategory == 8){
        setCity();
    }

    $("#btnUserQuery").click(function() {
        currentPageNo = 1;
        if(userCategory == 9) {
            queryListAll();
        }
        else if(userCategory == 8){
            queryList();
        }
    });
});

function sendpic() {
    var content = $("#picfile").val();
    if(content.length > 0) {
        var form = document.getElementById("picform");
        var formData = new FormData(form);
        upajax('/op/upload', formData, function (result) {
            result = JSON.parse(result);
            if(result.success != null){
                $("#picShow").attr("src", result.success);
                $("#picPath").val(result.success);
                $("#picfile").val('');
            }
            else if (result.error != null)
                alert(result.error);
        });
    }
    else
        alert('请您选择需要上传的图片！');
}

function setCity() {
    zhget(base_url_city, {
        id:userCity
    }, function(result) {
        var cityList = $("#city_id");
        citys = result.rows;
        var text = result.rows[0].name;
        var value = userCity;
        cityList.append("<option value='" + value + "'>" + text + "</option>");
        queryList();
    });
}

function queryCitys() {
    zhget(base_url_citys, {
        sort:'orderid'
    }, function(result) {
        var cityList = $("#city_id");
        citys = result.rows;
        for (var al in citys) {
            var text = citys[al].name;
            var value = citys[al].id;
            cityList.append("<option value='" + value + "'>" + text + "</option>");
        }
        queryListAll();
    });
}

function queryListAll() {
    zhget(base_url_indexAdvPic, {
        page: currentPageNo,
        size: pageRows
    }, function(result) {
        pics = result;
        for(var i = 0; i<pics.length; i++) {
            for(var j = 0 ; j < citys.length ; j++){
                if(result[i]['city_id']==citys[j]["id"]){
                    result[i]['city_name'] = citys[j]['name'];
                    break;
                }
            }
        }
        buildPlainTable(result, 'indexAdvPic-template', 'indexAdvPic-placeholder');
    });
}

function queryList() {
    zhget(base_url_indexAdvPic, {
        city_id:userCity,
        page: currentPageNo,
        size: pageRows
    }, function(result) {
        pics = result;
        for(var i = 0; i<pics.length; i++) {
            for(var j = 0 ; j < citys.length ; j++){
                if(result[i]['city_id']==citys[j]["id"]){
                    result[i]['city_name'] = citys[j]['name'];
                    break;
                }
            }
        }
        buildPlainTable(result, 'indexAdvPic-template', 'indexAdvPic-placeholder');
    });
}

function onAddClick() {
    cleanForm();
    operation = "add";
    $('#userModal').modal('show');
}

function onUpdateClick(id) {
    fillForm(id);
    operation = "modify";
    $('#userModal').modal('show');
}

function onDeleteClick(id) {
    if(confirm("确认要删除？")) {
        zhdelete(base_url_indexAdvPic + "/" + id, {
            AUTHNAME:authName
        }, function (result) {
            if (result && result.affectedRows > 0) {
                if(userCategory == 9) {
                    queryListAll();
                }
                else if(userCategory == 8){
                    queryList();
                }
                showSuccess('删除成功！');
            } else {
                showError('删除失败！');
            }
        });
    }
}

function onSaveClick() {
    var id = $("#id").val();
    var data = {
        AUTHNAME:authName,
        city_id:$("#city_id").val(),
        pubcom: $("#pubcom").val(),
        url: $("#targetUrl").val(),
        picpath: $("#picPath").val(),
        color: $("#color").val(),
        orders: $("#orders").val()
    };
    if (operation == "add") {
        zhpost(base_url_indexAdvPic, data, saveResult);
    } else {
        zhput(base_url_indexAdvPic + "/" + id, data, saveResult);
    }
}

function saveResult(result) {
    if (result) {
        if (result.outcome==0) {
            showError('保存失败！');
        } else{
            $('#userModal').modal('hide');
            if(userCategory == 9) {
                queryListAll();
            }
            else if(userCategory == 8){
                queryList();
            }
            showSuccess('保存成功！');
        }
    } else {
        showError('保存失败！');
    }
}

function fillForm(id) {
    var index = 0;
    for (index in pics) {
        var item = pics[index];
        if (item.id == id) {
            $("#picfile").val('');
            $("#picShow").attr("src",item.picpath);
            $("#id").val(item.id);
            $("#picPath").val(item.picpath);
            $("#targetUrl").val(item.url);
            $("#pubcom").val(item.pubcom);
            $("#color").val(item.color);
            $("#orders").val(item.orders);
            $("#city_id").val(item.city_id);
            return;
        }
    }
}

function cleanForm() {
    $("#id").val("");
    $("#picfile").val('');
    $("#picShow").attr("src","");
    $("#picPath").val("");
    $("#targetUrl").val("");
    $("#pubcom").val("");
    $("#color").val("");
    $("#orders").val("1");
}
